/**
 * Created by thinkl on 2017/6/19.
 */
$(function () {

    $.post('/commodity/query_commodity_class', {}, function (data) {
        var l = JSON.parse(data);
        var html = '';
        $.each(l.result, function (index, item) {
            html += '<option>' + item.classification + '</option>'
        });
        $('#commodity_classification').html(html)
    });
    $('#commodity_class_save').click(function () {
        var commodity_class = $('#commodity_class').val();
        $.post('/commodity/create_commodity_class', {commodity_classification: commodity_class}, function (data) {
            var lst = JSON.parse(data);
            if (lst.success) {
                location.reload()
            }
        })
    });
    var commodity_id = getQueryString('id');
    $.post('/commodity/get_commodity', {commodity_id: commodity_id}, function (data) {
        var l = JSON.parse(data);
        var html = '';
        var item = l.result;
        html += '<div class="commodity_line"><div class="left">商品名称</div><div class="right">' + item.name + '</div></div>' +
            '<div class="commodity_line"><div class="left">属性</div><div class="right">';
        $(item.p).each(function (index, obj) {
            html += '<label><input type="radio" name="property" value="' + obj.property_id + '">' + obj.property + '</label>';
        });
        html += '</div></div><div class="commodity_line"><div class="left">属性值</div><div class="right" id="radio_list"></div></div>';
        $('#commodity_name').html(html);
        $('input[type="radio"][name="property"]').click(function () {
            var n = $('input[type="radio"][name="property"]:checked').val();
            html = '';
            $(item.p).each(function (index, obj) {
                if (obj.property_id == n) {
                    $(obj.v).each(function (index, pro) {
                        html += '<span class="property_value">' + pro.value + '</span>'
                    });
                }
            });
            $('#radio_list').html(html);
        })
    });
});
function commodity_save() {
    var commodity_name = $('#commodity_name').val();
    var commodity_classification = $('#commodity_classification').val();
    var commodity_price = $('#commodity_price').val();
    var commodity_preferential = $('#commodity_preferential').val();
    var commodity = {
        commodity_name: commodity_name,
        commodity_classification: commodity_classification,
        commodity_price: commodity_price,
        commodity_preferential: commodity_preferential
    };
    $.post('/commodity/create_commodity', commodity, function (data) {
        var l = JSON.parse(data);
        if (l.success) {
            var commodity_id = l.result.id;
            location.href = '/static/create_commodity/create_property.html?id=' + commodity_id;
        }
    })
}
function create_property() {
    var commodity_property = $('#commodity_property').val();
    var commodity_id = getQueryString('id');
    $.post('/commodity/create_commodity_property', {
        commodity_id: commodity_id,
        commodity_property: commodity_property
    }, function (data) {
        var lst = (JSON).parse(data);
        if (lst.success) {
            location.reload()
        }
    });
}
function create_property_value() {
    var commodity_property_value = $('#commodity_property_value').val();
    var commodity_property_id = $('input[type="radio"][name="property"]:checked').val();
    $.post('/commodity/create_commodity_property_value', {
        commodity_property_id: commodity_property_id,
        commodity_property_value: commodity_property_value
    }, function (data) {
        var lst = (JSON).parse(data);
        if (lst.success) {
            location.reload()
        }
    });
}